﻿using System;
using Autofac;
using Microsoft.Extensions.Configuration;
using Surging.Core.Caching;
using Surging.Core.Caching.Configurations;
using Surging.Core.Codec.MessagePack;
using Surging.Core.Consul;
using Surging.Core.Consul.Configurations;
using Surging.Core.CPlatform;
using Surging.Core.CPlatform.Configurations;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.DotNetty;
using Surging.Core.EventBusRabbitMQ;
using Surging.Core.EventBusRabbitMQ.Configurations;
using Surging.Core.ProxyGenerator;
using Surging.Core.ServiceHosting.Internal;
using Surging.Core.ServiceHosting.Internal.Implementation;
using Surging.Core.System.Intercept;

namespace Surging.Base
{
    public static class SurgingExtension
    {
        public static IServiceHostBuilder RegisterMicroServices(this IServiceHostBuilder builder)
        {
            builder.RegisterServices(mybuilder =>
            {
                mybuilder.AddMicroService(option =>
                {
                    option.AddCache();
                    option.AddServiceRuntime(); //添加服务运行时服务
                    option.AddClientIntercepted(typeof(CacheProviderInterceptor)); //添加缓存拦截
                    option.AddRelateService(); //添加支持服务代理远程调用
                    option.AddConfigurationWatch(); //添加配置监控实时更新
                    option.UseConsulManager(); //使用Consul管理
                    option.UseDotNettyTransport(); //使用Netty传输
                    option.UseRabbitMQTransport(); //使用rabbitmq 传输
                    option.AddRabbitMQAdapt(); //基于rabbitmq的消费的服务适配
                    option.UseMessagePackCodec(); //基于MessagePack序列化
                    option.RegisterSwaggers();//构造swaggerjson
                    mybuilder.Register(p => new CPlatformContainer(ServiceLocator.Current)); //初始化注入容器
                });
            });
            return builder;
        }

        public static IServiceHostBuilder UseMicroService(this IServiceHostBuilder hostBuilder)
        {
            hostBuilder.UseServer(options =>
            {
                options.Token = "True";
                options.ExecutionTimeoutInMilliseconds = 30000;
                options.MaxConcurrentRequests = 200;
            });
            return hostBuilder;
        }

        public static IServiceHostBuilder RegConfigure(this IServiceHostBuilder builder,bool regConsul, bool regCache, bool regEvent)
        {
            builder.Configure(x =>
            {
                x.AddCPlatformFile("Configs/surgingSettings.json", optional: false, reloadOnChange: true);
                if (regConsul)
                {
                    x.AddConsulFile("Configs/consulSettings.json", optional: false);
                }
                if (regCache)
                {
                    x.AddCacheFile("Configs/cacheSettings.json", optional: false, reloadOnChange: true);
                }
                if (regEvent)
                {
                    x.AddEventBusFile("Configs/eventBusSettings.json", optional: false);
                }
            });
            return builder;
        }
    }
}
